﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RobotEscape.Entities;
using System.Windows.Media.Imaging;

namespace RobotEscape.Screens
{
    public class CompanyLogoScreen : ScreenBase
    {
        SpriteEntity spriteEntity;
        Sprite sprite;
        Rectangle backdropRect = new Rectangle();
        double startTime = 0;

        public CompanyLogoScreen()
        {
            Canvas.SetLeft(backdropRect, 0);
            Canvas.SetTop(backdropRect, 0);
            this.backdropRect.Width = Game.Instance.CANVAS_WIDTH;
            this.backdropRect.Height = Game.Instance.CANVAS_HEIGHT;
            Game.Instance.Canvas.Children.Add(backdropRect);


            sprite = new Sprite();
            sprite.file = "Assets/images/vi_logo2.png";
            sprite.fps = 50;
            sprite.frameCount = 20;
            sprite.height = 89;
            sprite.width = 960;
            sprite.loop = false;

            spriteEntity = new SpriteEntity();
            spriteEntity.width = 960;
            spriteEntity.height = 89;
            spriteEntity.PosY = backdropRect.Height / 2 - spriteEntity.height / 2;
            spriteEntity.pausable = true;
            AddEntity(spriteEntity);
        }

        public override void Update(GameTime time)
        {
            if (startTime == 0)
                startTime = time.TotalRealTime.TotalSeconds;

            if (time.TotalRealTime.TotalSeconds - startTime >= 3)
            {
                Game.Instance.SwitchScreen(new StartScreen());
            }

            WriteableBitmap wb = new WriteableBitmap(Game.Instance.CANVAS_WIDTH + 1, Game.Instance.CANVAS_HEIGHT);
            wb.Invalidate();
            wb.FillRectangle(0, 0, Game.Instance.CANVAS_WIDTH + 1, Game.Instance.CANVAS_HEIGHT, Color.FromArgb(255, 93, 93, 93));
            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.backdropRect.Fill = imageBrush;

            spriteEntity.Update(time, sprite);
            base.Update(time);
        }

        public override void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(backdropRect))
                Game.Instance.Canvas.Children.Remove(backdropRect);
            base.Remove();
        }
    }
}
